using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using AYE.BaseFramework.Manager;
using AYE.BaseFramework.Manager.Extensions;
using AYE_ClientSideAvalonia.Services;
using AYE_ClientSideAvalonia.ViewModels;
using AYE_ClientSideAvalonia.Views;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Navigation.Regions;

namespace AYE_ClientSideAvalonia;

public partial class App : PrismApplication
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);

        // Required when overriding Initialize
        // 在重写“初始化”方法时必需提供此参数。
        base.Initialize();
    }

    
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        #region 日志以及配置系统的注册必须放在最开始的地方,外加DB的注册
        containerRegistry.RegisterLogging();
        containerRegistry.RegisterConfiguration();
        containerRegistry.RegisterDatabase();
        //containerRegistry.RegisterAuthorityControl();
        #endregion

        // SidebarView 不用在这里显示注册，因为在 View中 显示指定了 prism:ViewModelLocator.AutoWireViewModel="True"

        containerRegistry.RegisterSingleton<INotificationService, NotificationService>();

        containerRegistry.RegisterForNavigation<DashboardView, DashboardViewModel>();
        containerRegistry.RegisterForNavigation<SettingsView, SettingsViewModel>();
        containerRegistry.RegisterForNavigation<SettingsSubView, SettingsSubViewModel>();
        containerRegistry.RegisterForNavigation<SidebarView, SidebarViewModel>();

        containerRegistry.RegisterForNavigation<WindowTestView, WindowTestViewModel>();
        containerRegistry.RegisterForNavigation<StartMainWindow, StartMainWindowViewModel>();

        containerRegistry.RegisterForNavigation<UCSidebarView, UCSidebarViewModel>();

        // Dialogs, etc.
    }

    /// <summary>
    /// 模块注册
    /// </summary>
    /// <param name="moduleCatalog"></param>
    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        // 这里是添加其他 类库的模块注册类中 注册行为
        // 最先添加的最先注册

        //1,底层框架
        moduleCatalog.AddModule<BaseFrameworkManagerModule>();
        base.ConfigureModuleCatalog(moduleCatalog);
    }

    /// <summary>Custom region adapter mappings.>自定义区域适配器映射。</summary>
    /// <param name="regionAdapterMappings">Region Adapters.</param>
    protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
    {
        regionAdapterMappings.RegisterMapping<ContentControl, ContentControlRegionAdapter>();
    }

    protected override AvaloniaObject CreateShell()
    {
        //return Container.Resolve<MainWindow>();//演示demo
        //return Container.Resolve<WindowTestView>();//测试
        return Container.Resolve<StartMainWindow>();//主窗口
    }

    /// <summary>
    /// IOC初始化之后 进行数据初始化
    /// 这里是 所有模块注册以及模块中的数据初始化之后，真正进入程序的最后的最后一步数据初始化
    /// 这里 需要将 区域注入的视图指定
    /// </summary>
    protected override void OnInitialized()
    {

        // Register Views to the Region it will appear in. Don't register them in the ViewModel.
        ////将视图注册到将要出现的区域。不要在ViewModel中注册它们。高层模块之间不应该相互依赖
        //// IOC中的区域管理器来统一管理区域注入,需要在这里提前将 区域名以及View 注册关联

        var regionManager = Container.Resolve<IRegionManager>();

        // WARNING: Prism v11.0.0-prev4
        // - DataTemplates MUST define a DataType or else an XAML error will be thrown
        // - Error: DataTemplate inside of DataTemplates must have a DataType set
        // 本质上 是将 string 与 具体的 视图View 关联起来
        regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(DashboardView));
        regionManager.RegisterViewWithRegion(RegionNames.SidebarRegion, typeof(SidebarView));

        regionManager.RegisterViewWithRegion(AyeClientSideAvaloniaRegionNames.SidebarRegion, typeof(UCSidebarView));

        ////regionManager.RegisterViewWithRegion(RegionNames.DynamicSettingsListRegion, typeof(Setting1View));
        ////regionManager.RegisterViewWithRegion(RegionNames.DynamicSettingsListRegion, typeof(Setting2View));
    }

    
}
